package cn.com.text.Activity;

import android.Manifest;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.health.SystemHealthManager;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.Random;

import cn.com.text.Dao.MyPost;
import cn.com.text.Dao.News;
import cn.com.text.MainActivity;
import cn.com.text.R;
import cn.com.text.Util.ImageHelper;
import cn.com.text.Util.MySQLConnections;

public class PublishPost extends AppCompatActivity {
    private Uri imageUri;
    private static int number = 0;
    private ImageView imageView;
    private EditText editText1;
    private EditText editText2;
    private EditText editText3;
    private Toolbar toolbar;
    private Button button;
    private final int CODE_PICK_PHOTO = 0;
    private final int RESULT_REQUEST_CODE = -1;
    private String bitmapToString = null;
    private String photoPath = null;
    private Bitmap bitmap;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_post);
        Intent intentRec = getIntent();
        username = intentRec.getStringExtra("userName");
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        final Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                return false;
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();//message用于给handler传递参数
                try {
                    MySQLConnections.getConnection("Blog");
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d("TAG", " 数据操作异常");
                }
                handler.sendMessage(msg);
            }
        }).start();
        imageView = (ImageView) findViewById(R.id.imageUp);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_gallery = new Intent(Intent.ACTION_PICK);
                intent_gallery.setType("image/*");
                startActivityForResult(intent_gallery, CODE_PICK_PHOTO);
            }
        });
        editText1 = (EditText) findViewById(R.id.upAuthor);
        editText2 = (EditText) findViewById(R.id.uptitle);
        editText3 = (EditText) findViewById(R.id.upcontent);
        button = (Button) findViewById(R.id.buttonPub);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String title = editText1.getText().toString();
                final String author = editText2.getText().toString();
                final String content = editText3.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            MySQLConnections.insertIntoData(PublishPost.number++, title, author, content, bitmapToString);//调用插入数据库语句
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                Saving(title, content, username, bitmapToString);
                Intent intent = new Intent(PublishPost.this, MainActivity.class);
                intent.putExtra("postTitle", title);
                intent.putExtra("UNAME", username);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        toolbar = (Toolbar) findViewById(R.id.toolbarPub);
        toolbar.setTitle("发帖");
        toolbar.setSubtitle("GaoMing");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void Saving(String title, String content, String username, String bitmapToString) {
        MyPost mypost = new MyPost();
        mypost.setNewstitle(title);
        mypost.setNewscontent(content);
        mypost.setUsername(username);
        mypost.setImagePost(bitmapToString);
        boolean is_ok = mypost.save();

        Log.d("Answer:", String.valueOf(is_ok));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CODE_PICK_PHOTO) {//相册
            imageUri = data.getData();
            //获取照片路径
            imageView.setImageURI(imageUri);
            Bitmap bitmap = null;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                bitmapToString = ImageHelper.bitmapToString(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


